package top.leinx.tmind.component;

import java.util.ArrayList;
import java.util.List;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;

/**
 * 项目名称: MindMap
 * 创建时间: 2021/4/15 13:57
 * 作者姓名: xiaolei
 */

public final class MindSubject implements java.io.Serializable {
    private byte[] icon;    // 图标
    private String text;    // 文字
    private Double x;       // x 坐标
    private Double y;       // y 坐标
    private String styleClass;                            // JavaFX 样式类
    private List<MindSubject> items = new ArrayList<>();  // 子主题
    private MindSubject parent;     // 父主题，中心主题的父主题为 null

    private int postion;            // 在中心主题的左侧还是右侧

    private transient Pane pane;    // 绘制主题的窗格
    private transient Label label;  // 绘制需要的标签，存储时不保存

    private boolean selected;       // 是否选中当前主题
    private int numberOfLeaf;

    /**
     * 构造方法
     *
     * @param text 主题的文本
     */
    public MindSubject(Pane pane, MindSubject parent, String text, int postion) {
        this.pane = pane;
        this.parent = parent;
        this.text = text;
        this.postion = postion;
        buildLabel();
    }

    private void buildLabel() {
        // TODO: 先不处理图标
        if (this.label == null) {
            this.label = new Label(text);
        }
        this.label.setUserData(this);
        this.setSelected(false);
    }

    public int getNumberOfLeaf(int postion) {
        if (this.postion != postion) {
            return 0;
        }
        if (this.items.size() == 0) {
            return 1;
        } else {
            int total = 0;
            for (MindSubject s : items) {
                total += s.getNumberOfLeaf(postion);
            }
            return total;
        }
    }

    // ----------------------------------------------------------------
    public byte[] getIcon() {
        return icon;
    }

    public void setIcon(byte[] icon) {
        this.icon = icon;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        this.label.setText(text);
    }

    public Double getX() {
        return x;
    }

    public void setX(Double x) {
        this.x = x;
        this.label.setLayoutX(x);
    }

    public Double getY() {
        return y;
    }

    public void setY(Double y) {
        this.y = y;
        this.label.setLayoutY(y);
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public List<MindSubject> getItems() {
        return items;
    }

    public MindSubject getParent() {
        return parent;
    }

    public void setParent(MindSubject parent) {
        this.parent = parent;
    }

    public Label getLabel() {
        return label;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        label.getStyleClass().removeAll("node-normal", "node-selected");
        if (selected) {
            label.getStyleClass().add("node-selected");
        } else {
            label.getStyleClass().add("node-normal");
        }
    }

    public int getPostion() {
        return postion;
    }

    public void setPostion(int postion) {
        this.postion = postion;
    }
}
